package cn.touna.jss.modules.third.baihang.utils;

import cn.touna.jss.common.util.CommonUtils;
import cn.touna.jss.modules.third.baihang.sdk.BhCreditApiClient;
import cn.touna.jss.modules.third.baihang.sdk.encrypt.RSAUtil;
import cn.touna.jss.modules.third.baihang.sdk.request.InterfaceUploadRequest;
import cn.touna.jss.modules.third.baihang.sdk.response.InterfaceUploadResponse;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.BasicScheme;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: xiaoli
 * @date: 2019/2/21 17:27
 * @Desc:
 **/
public class BaiHangUtil {


    public static String  encrypt(String keyPath,String text) throws Exception{
        //RSA公钥文件路径（百行提供公钥文件）
//        String RSA_PUBLIC_KEY = "d:\\rsa_public_key_touna.pem";

        InterfaceUploadRequest req = new InterfaceUploadRequest();
        List<String> datas = new ArrayList<String>();
        datas.add(text);

        //设置待加密的原始数据集合
        req.setData(datas);
        BhCreditApiClient client = new BhCreditApiClient();
        //初始化设置RSA公钥
        client.init(RSAUtil.readRSAPublicKey(keyPath));
        //执行加密操作
        InterfaceUploadResponse response = client.execute(req);
        if(response.isSuccess){
            List<String> data = response.getEncryptData();
            for(int i=0;i<data.size();i++){
                return data.get(i);
            }
        }
        return "";
    }



    /**
     * 基于username和password的basic授权
     *
     * @param username
     * @param password
     * @return 返回HTTP Header Authorization的值
     */
     public static String authorization(String username, String password) {
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            throw new IllegalArgumentException("username or password is empty!!!");
        }
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
        return new BasicScheme().authenticate(credentials, CommonUtils.UTF8);
    }


    public static void main(String[] args) {
        String str = authorization("SZTN_A0001","yff3445683");
        System.out.println(str);
    }




}
